package org.dut.book.Service;

import org.dut.book.Enum.BookStatus;
import org.dut.book.Mapper.BookMapper;
import org.dut.book.model.BookInfo;
import org.dut.book.model.PageRequest;
import org.dut.book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public PageResult getBookListByPage(PageRequest pageRequest) {
        //从数据库获取图书
        List<BookInfo> list = bookMapper.selectBookInfoByPage(pageRequest);
        for(BookInfo book : list){
            book.setStatusCN(BookStatus.getStatusByCode(book.getStatus()).getDesc());
        }
        //计算图书总量
        Integer count =bookMapper.countAllBookInfo();
        return new PageResult(list,count,pageRequest);
    }

    public Integer addBook(BookInfo book) {
        return bookMapper.insertBook(book);
    }

    public BookInfo queryBookById(Integer id) {
        return bookMapper.selectBookById(id);
    }

    public Integer updateBook(BookInfo book) {
        return bookMapper.updateBook(book);
    }

    public Boolean deleteBookById(Integer id) {
        return bookMapper.deleteBookById(id);
    }

    public Integer deleteBookByIds(Integer[] ids) {
        return bookMapper.deleteBookByIds(ids);
    }
}
